package chances.wechat.applet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import chances.epg.entity.user.WechatUser;
import chances.epg.user.service.WechatUserService;
import chances.wechat.config.WeChatMvcConfig;
import chances.wechat.service.GatewayService;


@Controller
@RequestMapping(path="/applet")
public class AppletLoginCtrl {
	
	@Autowired
	WeChatMvcConfig weChatMvcConfig;
	
	@Autowired
	WechatUserService wechatUserService;
	
	@Autowired
	private GatewayService gatewayService;
	
	/**
	 * 登录方法，入口
	 * @param app_code 必填 用于获取小程序的AppID
	 * @param code 必填 小程序登录时获得的用户登录凭证（有效期五分钟），用于获取ession_key
	 * @return 
	 * @return 返回登录结果
	 * @throws IOException 
	 */
	@RequestMapping(path="/userInfo",method=RequestMethod.POST,  produces="application/json")
	public void login(HttpServletRequest request,HttpServletResponse response,@RequestBody WechatUser user) throws IOException{

		String result = gatewayService.postJson(request, response, "/applet/getOpenId",user);
		response.setHeader("Content-type", "application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
		response.getWriter().append(result);
		
	}
}
